<template>
	<view>

		<view v-for="(item,index) in sendList" :key="index" class="m-20 bg-white bb p-20 br-6 shadow-1">
			<view class="flex ac sb">
				<view class="flex">
					<view>
						<image class="br-100p" :src="item.avatar" mode="aspectFill" style="width: 80rpx;height: 80rpx;">
						</image>
					</view>
					<view class="ml-10 mt-5">
						<view>
							<text class="fw fs-30">{{item.name || ''}}</text>
							<text class="ml-10 mr-10">/</text>
							<text class="fs-24 c-666">{{item.position || ''}}</text>
							<text v-if="item.is_real == 2" class="iconfont-yjgs c-999 ml-10">&#xe6eb;</text>
							<text v-if="item.is_real == 1" class="iconfont-yjgs ml-10"
								style="color: #00009E;">&#xe6eb;</text>
						</view>
						<view class="c-999 mt-5 fs-24 line-1">
							{{item.company_name || ''}}
						</view>
					</view>
				</view>
				<navigator :url="'/pages/extend/connection/index_desc?id='+item.card_id" hover-class="none">
					<view class="box-fast">
						立即联系
					</view>
				</navigator>
			</view>
			<view class="mt-10" style="margin-left: 90rpx;">
				<view class="flex ac">
					<view v-if="item.type == 2" class="box-xq">
						需求
					</view>
					<view v-else class="box-xq"
						style="background-color: #FFE7E6;color: #D22C25;border: 1rpx solid #D22C25;">
						供应
					</view>
					<view class="fw fs-30 ml-10 line-2" style="width: 80vw;">
						{{item.title || ''}}
					</view>
				</view>
				<view class="mt-10 ">
					<u-read-more toggle :showHeight='140' textIndent='0em'>
						<view>
							<text class="fs-28">
								{{item.content}}
							</text>
						</view>
					</u-read-more>
				</view>
				<view v-if="item.images" class="mt-20 box-img-for">
					<view @click="$timer.preView($timer.strToArr($timer.ensureUrl(item.images)),_index)"
						v-for="(obj,_index) in $timer.strToArr($timer.ensureUrl(item.images))" :key="_index"
						class="mb-5 mr-15">
						<image class="br-6" style="height: 160rpx;width: 160rpx;" :src="obj" mode="aspectFill">
						</image>
					</view>
				</view>
				<view class="flex ac">
					<view class="mr-50">
						<text class="fs-24 c-999">{{$timer.formatTime(item.create_time) || ''}}</text>
					</view>
					<view class="ml-30">
						<text class="iconfont icon-pinglun3"></text>
						<text class="ml-5 fs-24">{{item.comment_num || 0}}</text>
					</view>
					<view v-if="item.is_follow == 2" @click="followClick('收藏',item)" class="ml-50">
						<text class="iconfont icon-shoucang2"></text>
						<text class="ml-5 fs-24">{{item.follow_num || 0}}</text>
					</view>
					<view v-if="item.is_follow == 1" @click="followClick('取消收藏',item)" class="ml-50">
						<text class="iconfont-yjgs fs-32" style="color: #FBC11A;">&#xe62a;</text>
						<text class="ml-5 fs-24">{{item.follow_num || 0}}</text>
					</view>
					<view @click="cardCardInfoLikeFunc(item)" class="ml-50">
						<text class="iconfont icon-dianzan1"></text>
						<text class="ml-5 fs-24">{{item.like_num || 0}}</text>
					</view>
					<view @click="extendClick(item)" class="ml-50 box-moreM">
						<text class="iconfont icon-gengduo1"></text>
					</view>
				</view>
				<view v-if="item.comment.length > 0" class="mt-20  bb p-10 fs-24" style="background-color: #F5F5F5;">
					<view @click="replyCommont(item,cur)" v-for="(cur,_ind) in item.comment" :key="_ind" class="mb-10">
						<text style="color: #00009E;" class="fw">{{cur.user.nickname || ''}}</text>
						<text v-if="!cur.re_uid" class="mr-5">:</text>
						<text v-if="cur.re_uid" class="ml-5 ml-5">回复</text>
						<text v-if="cur.re_uid" style="color: #00009E;"
							class="fw ">{{cur.reuser.nickname || ''}}：</text>
						<text>{{cur.content || ''}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 空数据站位 -->
		<emptyPage v-if="sendList.length == 0" style="margin-top: 0rpx;margin-bottom: 20rpx;">
		</emptyPage>
		<!-- end -->

		<!-- 加载更多 -->
		<uni-load-more v-if="sendList.length > 0" :status="status" />
		<!-- end -->

		<!-- 供需对接 扩展 -->
		<u-action-sheet @select='sheetSelect' cancelText='取消' round='12' @close='sheetClose' closeOnClickOverlay
			safeAreaInsetBottom :actions="sheetList" :show="sheetShow"></u-action-sheet>
		<!-- end -->

		<!-- 弹窗输入(供需对接 评论) -->
		<u-popup :show="popupShow" @close="popupShow=false;popupFocus=false">
			<view class="p-20 flex ac">
				<view class="w-100p">
					<u--input :focus='popupFocus' :cursorSpacing='120' :placeholder="placeholder" border="surround"
						v-model="inputValu"></u--input>
				</view>
				<view v-if="popupShow" @click="sendCommonClick" class="sentCommon">
					发送
				</view>
			</view>
		</u-popup>
		<!-- end -->

		<!-- 供需发布 -->
		<view @click="toPageClick" class="box-gxfb">
			<image src="https://yjgs.jsonbug.com/storage/default/20241113/20241114112212.png" mode="aspectFill"
				style="width: 120rpx;height: 120rpx;"></image>
		</view>
		<!-- end -->

	</view>
</template>

<script>
	import Cache from '@/utils/cache.js';
	import {
		mapGetters
	} from "vuex";
	import {
		configMap
	} from "@/utils";
	import {
		cardCardInfoLike,
		cardUserGetCardStatus,
		cardCardInfoList,
		projectCommentCreate,
		projectCommentDelete,
		cardRelationCreate,
		cardRelationDelete
	} from '@/api/extend.js'
	import {
		toLogin
	} from '@/libs/login.js';
	export default {
		data() {
			return {
				current_page: 1,
				inputValu: '',
				sheetList: [{
						name: '评论',
					},
					{
						name: '转发',
						openType: 'share'
					},
				],
				sheetShow: false,
				placeholder: '请输入内容！',
				popupShow: false,
				popupFocus: false,
				sendList: [],
				status: 'loading', // more loading noMore
				sendPage: {
					current_page: 1,
				},
				form: {
					search: ''
				},
			}
		},
		computed: configMap({}, mapGetters(['isLogin', 'uid', 'userInfo'])),
		onShareAppMessage: function() {
			let that = this;
			wx.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline']
			});
			return {
				title: '供需信息',
				imageUrl: "https://yjgs.jsonbug.com/storage/default/20241220/share.png",
				path: '/pages/extend/connection/supply_desc?uid=' + (that.uid || -1)
			};
		},
		onShareTimeline: function() {
			let that = this;
			return {
				title: '供需信息',
				query: {
					uid: this.uid || -1
				},
				imageUrl: 'https://yjgs.jsonbug.com/storage/default/20241220/share.png'
			};
		},
		onReachBottom() {
			if (this.status == 'noMore') return;
			this.sendPage.current_page++;
			this.cardCardInfoListFunc();
		},
		methods: {
			
			cardStatusFunc() {
				return new Promise((resolve) => {
					cardUserGetCardStatus().then(res => {
						if (res.status == 200 && res.data) {
							uni.setStorageSync('cardExpireTime', res.data);
							
							if(res.data && res.data.card_endtime){
								let cruurenTime = this.$timer.formatTime(Date.now(), 'yyyy-mm-dd hh:MM:ss');
								let isExpired = this.$timer.isMembershipExpired(cruurenTime, res.data.card_endtime);
								// 如果会员未过期且是会员，返回true
								resolve(!isExpired && res.data.is_card == 1);
							}else{
								// 没有结束时间或不是会员
								resolve(false);
							}
							
						} else {
							resolve(false);
						}
					}).catch(() => {
						resolve(false);
					});
				});
			},
			/**
			 * 判断一下会员是否过期
			 */
			async verifyCardIsExpire() {
				try {
					let data = uni.getStorageSync('cardExpireTime');
					if (data) {
						if (data.is_card == 1) { // 判断一下是否过期了
							let cruurenTime = this.$timer.formatTime(Date.now(), 'yyyy-mm-dd hh:MM:ss');
							let isExpired = this.$timer.isMembershipExpired(cruurenTime, data.card_endtime);
							if (isExpired) {
								// 如果过期了，重新获取会员状态
								return await this.cardStatusFunc();
							}
							return true; // 会员未过期
						} else {
							// 不是会员，重新获取状态确认
							return await this.cardStatusFunc();
						}
					} else {
						// 没有缓存数据，获取会员状态
						return await this.cardStatusFunc();
					}
				} catch (error) {
					console.error('验证会员状态出错:', error);
					return false;
				}
			},
			toPageClick() {

				// 判断登录
				if (!this.isLogin) {
					toLogin();
					return;
				}

				// 不是会员提示
				this.verifyCardIsExpire().then(isVip => {
					if (!isVip) {
						uni.showModal({
							content: '仅限遇见贵商会员发布',
							confirmText: '开通会员',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/extend/user_vip/user_vip'
									})
								}
							}
						})
						return;
					}
					
					uni.navigateTo({
						url: '/pages/extend/connection/send_need'
					})
				});
			},
			sendCommonClick() {

				if (!this.inputValu) {
					uni.showToast({
						title: '请输入内容',
						icon: 'none'
					})
					return;
				}

				projectCommentCreate({
					content: this.inputValu,
					re_uid: this.replyComment ? this.replyComment.uid : '',
					project_id: this.currentItem.id,
					type: 2
				}).then(res => {
					this.popupShow = false;
					let userInfo = Cache.get('USER_INFO', true);
					if (res.status == 200) {
						let sendListIndex = this.sendList.findIndex(item => item.id == this.currentItem.id);
						this.sendList[sendListIndex].comment.push({
							id: res.data.id,
							content: this.inputValu,
							re_uid: this.replyComment ? this.replyComment.re_uid : '',
							reuser: this.replyComment ? this.replyComment.reuser : {},
							uid: this.uid,
							user: {
								uid: this.uid,
								nickname: userInfo.nickname || '用户'
							}
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})


			},
			sheetClose() {
				this.sheetShow = false;
				this.sheetList = [{
						name: '评论',
					},
					{
						name: '转发',
						openType: 'share'
					},
				];
			},
			sheetSelect(e) {
				if (e.name == '评论') {
					if (!this.isLogin) {
						toLogin();
						return;
					}
					this.replyComment = {};
					this.inputValu = '';
					this.placeholder = `请输入内容！`;
					this.popupShow = true;
					this.$nextTick(() => {
						this.popupFocus = true;
					})
					return
				}

				if (e.name == '删除') {
					if (!this.isLogin) {
						toLogin();
						return;
					}
					this.sheetList = false;
					projectCommentDelete({
						id: this.replyComment.id,
					}).then(res => {
						if (res.status == 200) { // 删除页面数据
							let sendListIndex = this.sendList.findIndex(item => item.id == this.currentItem.id);
							let replyIndex = this.sendList[sendListIndex].comment.findIndex(item => item.id == this
								.replyComment.id);
							this.sendList[sendListIndex].comment.splice(replyIndex, 1);
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					})

				}
			},
			extendClick(item) {
				this.sheetShow = true;
				this.currentItem = item
			},
			replyCommont(item, cur) {
				this.replyComment = cur;
				this.currentItem = item

				if (cur.uid == this.uid) {
					this.sheetList = [{
						name: '删除'
					}];
					this.sheetShow = true;
					return
				}
				this.inputValu = '';
				this.placeholder = `回复 ${cur.user.nickname}`;
				this.popupShow = true;
				this.popupFocus = true;
			},
			followClick(tag, item) {
				if (tag == '收藏') {
					cardRelationCreate({
						type_id: item.id,
						type: 15
					}).then(res => {
						if (res.status == 200) {
							item.is_follow = 1;
							item.follow_num++
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					})
				} else {
					cardRelationDelete({
						type_id: item.id,
						type: 15
					}).then(res => {
						if (res.status == 200) {
							item.is_follow = 2;
							item.follow_num--
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					})
				}
			},
			cardCardInfoLikeFunc(item) {
				cardCardInfoLike({
					id: item.id
				}).then(res => {
					if (res.status == 200) {
						item.like_num++;
					}
				})
			},
			cardCardInfoListFunc() {
				this.status = 'loading';
				cardCardInfoList({
					type: '',
					industry: '',
					keyword: this.form.search,
					limit: 10,
					page: this.sendPage.current_page
				}).then(res => {
					let data = res.data.data;
					this.sendPage.current_page = res.data.current_page;
					if (data.length < 6) {
						this.status = 'noMore'
					} else {
						this.status = 'more'
					}
					this.sendList = [...this.sendList, ...data];
					uni.stopPullDownRefresh();
				})
			},
		},
		created() {
			this.cardCardInfoListFunc();
		}
	}
</script>

<style scoped lang="scss">
	.box-img-for {
		display: flex;
		flex-wrap: wrap;
	}

	.box-fast {
		text-align: center;
		font-size: 28rpx;
		line-height: 60rpx;
		width: 140rpx;
		height: 60rpx;
		box-sizing: border-box;
		background-color: #E1E1FC;
		color: #00009E;
		font-weight: bold;
		border-radius: 6rpx;
	}

	.box-gxfb {
		z-index: 999;
		position: fixed;
		right: 20rpx;
		bottom: 220rpx;
		border-radius: 100%;
	}

	.box-xq {
		border-radius: 6rpx;
		border: 1rpx solid #00009E;
		color: #00009E;
		font-weight: bold;
		text-align: center;
		font-size: 26rpx;
		width: 80rpx;
		height: 40rpx;
		background-color: #F1F1FF;
	}

	.box-moreM {
		text-align: center;
		line-height: 40rpx;
		width: 60rpx;
		height: 40rpx;
		background-color: #F5F5F5;
		border-radius: 6rpx;
	}

	.sentCommon:active {
		opacity: .5;
	}

	.sentCommon {
		border-radius: 12rpx;
		margin-left: 20rpx;
		width: 120rpx;
		height: 65rpx;
		background-color: #00009E;
		color: white;
		text-align: center;
		line-height: 65rpx;
	}
</style>